package com.aysafety.eventmodel.corn;

import com.aysafety.eventmodel.common.DateUtils;
import com.aysafety.eventmodel.service.electromagnetic.ElectromagneticService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 电磁辐射数据生产
 * @Param:
 * @Author: ChengQi
 * @Date: 2018/12/17
 */
//@Component
public class ElectromagneticRadiationProducerTask {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ElectromagneticService electromagneticService;

    /**
     * @Description: 电磁
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2018/12/17
     */
//    @Scheduled(cron = "0 0/1 * * * ? ")
    public void electromagneticRadiationWarning() {
        logger.info("正在计算电磁辐射...");
        try {
            String currentTime = DateUtils.getTime();
            electromagneticService.electromagneticRadiationWarning(currentTime);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成电磁辐射计算...");
    }

//    @Scheduled(cron = "30 35 10 28 * ?")
    public void soundEmissionWarningHistory() {
        logger.info("正在计算能量平均值...");
        try {
            Calendar c = Calendar.getInstance();
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 开始时间必须小于结束时间
            Date beginDate = dateFormat1.parse("2019-02-01 21:19:00");
            Date endDate = dateFormat1.parse("2019-02-28 10:30:00");
            Date date = beginDate;
            while (!date.equals(endDate)) {
                String currentTime = DateUtils.getTime(date);
                electromagneticService.electromagneticRadiationWarning(currentTime);
                c.setTime(date);
                c.add(Calendar.MINUTE, 1);
                date = c.getTime();
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成能量平均值计算...");
    }

    /**
     * @Description: 综合预警
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2018/12/29
     */
    //@Scheduled(cron = "0 42 11 2 * ? ")
//    @Scheduled(cron = "0 33 23 8 * ?")
    public void dailyComprehensiveWarning() {

        logger.info("电-正在计算综合预警...");
        try {
            //String currentTime = DateUtils.getTime();
            Calendar c = Calendar.getInstance();
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //开始时间必须小于结束时间
            Date beginDate = dateFormat1.parse("2018-12-10 00:00:00");
            Date endDate = dateFormat1.parse("2018-12-30 00:00:00");
            Date date = beginDate;
            while (!date.equals(endDate)) {
                String currentTime = DateUtils.getTime(date);
                electromagneticService.dailyComprehensiveWarning(currentTime);
                c.setTime(date);
                c.add(Calendar.HOUR_OF_DAY, 1); // 日期加1天
                date = c.getTime();
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成电-综合预警计算...");

    }

}
